/*
 简述：Cocoa框架中的NSPredicate用于查询，原理和用法都类似于SQL中的where，作用相当于数据库的过滤取。
 定义(最常用到的方法)：
 [cpp] view plain copy
 NSPredicate *ca = [NSPredicate predicateWithFormat:(NSString *), ...];
 Format：
 (1)比较运算符>,<,==,>=,<=,!=
 可用于数值及字符串
 例：@"number > 100"
 
 
 (2)范围运算符：IN、BETWEEN
 例：@"number BETWEEN {1,5}"
 @"address IN {'shanghai','beijing'}"
 
 
 (3)字符串本身:SELF
 例：@“SELF == ‘APPLE’"
 
 
 (4)字符串相关：BEGINSWITH、ENDSWITH、CONTAINS
 例：@"name CONTAIN[cd] 'ang'"   包含某个字符串
 @"name BEGINSWITH[c] 'sh'"     以某个字符串开头
 @"name ENDSWITH[d] 'ang'"      以某个字符串结束
 注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写，也不区分发音符号。
 
 
 (5)通配符：LIKE
 例：@"name LIKE[cd] '*er*'"    *代表通配符,Like也接受[cd].
 @"name LIKE[cd] '???er*'"
 
 (6)正则表达式：MATCHES
 例：NSString *regex = @"^A.+e$";   以A开头，e结尾
 @"name MATCHES %@",regex
 */

/*
 正则表达式
 NSRegularExpressionOptions
 NSRegularExpressionCaseInsensitive             = 1 << 0, //不区分字母大小写的模式
 NSRegularExpressionAllowCommentsAndWhitespace  = 1 << 1, //忽略掉正则表达式中的空格和#号之后的字符
 NSRegularExpressionIgnoreMetacharacters        = 1 << 2, //将正则表达式整体作为字符串处理
 NSRegularExpressionDotMatchesLineSeparators    = 1 << 3, //允许.匹配任何字符，包括换行符
 NSRegularExpressionAnchorsMatchLines           = 1 << 4, //允许^和$符号匹配行的开头和结尾
 NSRegularExpressionUseUnixLineSeparators       = 1 << 5, //设置\n为唯一的行分隔符，否则所有的都有效。
 NSRegularExpressionUseUnicodeWordBoundaries    = 1 << 6 //使用Unicode TR#29标准作为词的边界，否则所有传统正则表达式的词边界都有效
 */

import UIKit

extension NSString {
    
    var isZipOrPostalCode: Bool {
        var result = false
        let postalRegExString = "(^\\d{5}(-\\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$)"
        var postalRegex: NSRegularExpression?
        do {
            postalRegex = try NSRegularExpression(pattern: postalRegExString, options: .CaseInsensitive)
        } catch { }
        
        result = postalRegex?.numberOfMatchesInString(self as String, options: .ReportProgress, range: NSMakeRange(0, length)) > 0
        
        return result
    }
    
    /** 验证 Email */
    var isValidEmail: Bool {
        let emailRegex = ".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*"
        let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
        
        return emailTest.evaluateWithObject(self)
    }
    
    /** 正则判断手机号码地址格式 */
    var isValidMobileNumber: Bool {
        /**
         * 手机号码
         * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         * 联通：130,131,132,152,155,156,185,186
         * 电信：133,1349,153,180,189
         */
        let MOBILE = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
        /**
         10         * 中国移动：China Mobile
         11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         12         */
        let CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
        /**
         15         * 中国联通：China Unicom
         16         * 130,131,132,152,155,156,185,186
         17         */
        let CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$";
        /**
         20         * 中国电信：China Telecom
         21         * 133,1349,153,180,189
         22         */
        let CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$";
        /**
         25         * 大陆地区固话及小灵通
         26         * 区号：010,020,021,022,023,024,025,027,028,029
         27         * 号码：七位或八位
         28         */
        // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
        
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@", MOBILE)
        let regextestcm = NSPredicate(format: "SELF MATCHES %@", CM)
        let regextestcu = NSPredicate(format: "SELF MATCHES %@", CU)
        let regextestct = NSPredicate(format: "SELF MATCHES %@", CT)
        
        if regextestcm.evaluateWithObject(self) {
            print("China Mobile")
        } else if regextestcu.evaluateWithObject(self) {
            print("China Unicom")
        } else if regextestct.evaluateWithObject(self) {
            print("China Telecom")
        } else { print("Unknow") }
        
        return regextestmobile.evaluateWithObject(self)
            || regextestcm.evaluateWithObject(self)
            || regextestcu.evaluateWithObject(self)
            || regextestct.evaluateWithObject(self)
    }
    
    /** 验证字母开头 */
    var isValidLetterPrefix: Bool {
        let regex = "[A-Za-z]+"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)

        return predicate.evaluateWithObject(self)
    }
    
    /** 验证只能是数字 */
    var isValidNumber: Bool {
        let regex = "^[0-9]+$";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    /** 验证身份证 */
    var isIDCard: Bool {
        let regex = "\\d{14}[[0-9],0-9xX]";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    /** 验证只能是字母 */
    var isValidOnlyLetter: Bool {
        let regex = "^[A-Za-z0-9]+$";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    /** 验证整数和小数 */
    var isValidNumberOrDecimals: Bool {
        let regex = "^[0-9]+([.]{0,1}[0-9]+){0,1}$";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    /** 验证只能输入汉字 */
    var isValidOnlyChinese: Bool {
        let regex = "^[\\u4e00-\\u9fa5]{0,}$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    /** 验证URL */
    var isValidURL: Bool {
        let regex = "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    /** 验证月份 */
    var isValidMonth: Bool {
        let regex = "^(0?[1-9]|1[0-2])$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    /** 验证日期 */
    var isValidDay: Bool {
        let regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
}
